package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Approval;
import com.zyn.rsgl.pojo.Leave;
import com.zyn.rsgl.pojo.Overtime;
import com.zyn.rsgl.pojo.Reimbursement;
import com.zyn.rsgl.service.ApprovalService;
import com.zyn.rsgl.service.LeaveService;
import com.zyn.rsgl.service.OvertimeService;
import com.zyn.rsgl.service.ReimbursementService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 审批表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/rsgl/approval")
public class ApprovalController {
    @Resource
    private ApprovalService approvalService;
    @Resource
    private LeaveService leaveService;
    @Resource
    private OvertimeService overtimeService;
    @Resource
    private ReimbursementService reimbursementService;
    @Resource
    private ReimbursementController reimbursementController;

    // 分页查询
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String status
    ) {
        return approvalService.listPage(pageNum, pageSize, userId, startTime, status);
    }

    // 获取单个审批信息
    @GetMapping("/get/{approvalId}")
    public R get(@PathVariable("approvalId") String approvalId) {
        Approval byId = approvalService.getById(approvalId);
        if (byId != null)
            return R.ok(byId);
        return R.fail("审批信息不存在");
    }

    // 添加审批信息
    @PostMapping("/add")
    public R add(@RequestBody Approval approval) {
        if(approval.getUserId() == null || approval.getType() == null){
            return R.fail("审批信息不完整");
        }
        // 判断是否存在
        if (approvalService.getById(approval.getId()) != null)
            return R.fail("审批信息已存在");
        approval.setStatus("待审批");
        return approvalService.save(approval) ? R.ok() : R.fail("添加失败");
    }

    // 修改审批信息
    @PutMapping("/update")
    public R update(@RequestBody Approval approval) {
        //校验审批天数
        if ("请假".equals(approval.getType()) || "加班".equals(approval.getType())) {
            if (approval.getDuration() < 1) {
                return R.fail("审批天数错误！");
            }
        }
        else approval.setDuration(null);

        String originalId = approval.getOriginalId();
        String newStatus = approval.getStatus();
        switch (approval.getType()) {
            case "请假":
                Leave leave = leaveService.getById(originalId);
                leave.setStatus(newStatus);
                leaveService.updateById(leave);
                break;
            case "加班":
                Overtime overtime = overtimeService.getById(originalId);
                overtime.setStatus(newStatus);
                overtimeService.updateById(overtime);
                break;
            case "补卡":
                Reimbursement reimbursement = reimbursementService.getById(originalId);
                reimbursement.setStatus(newStatus);
                reimbursementController.update(reimbursement);
                break;
            default:
                throw new RuntimeException("未知的审批类型");
        }
        return approvalService.updateById(approval) ? R.ok() : R.fail("修改失败");
    }

    // 删除审批信息
    @DeleteMapping("/delete/{approvalId}")
    public R delete(@PathVariable("approvalId") String approvalId) {
        Approval approval = approvalService.getById(approvalId);
        String originalId = approval.getOriginalId();
        switch (approval.getType()) {
            case "请假":
                Leave leave = leaveService.getById(originalId);
                leaveService.removeById(leave);
                break;
            case "加班":
                Overtime overtime = overtimeService.getById(originalId);
                overtimeService.removeById(overtime);
                break;
            case "补卡":
                Reimbursement reimbursement = reimbursementService.getById(originalId);
                reimbursementService.removeById(reimbursement);
                break;
            default:
                throw new RuntimeException("未知的审批类型");
        }
        return approvalService.removeById(approvalId) ? R.ok() : R.fail("删除失败");
    }
}
